<?php
namespace app\models;
use yii\behaviors\TimestampBehavior;
use yii\base\Model;
use yii\base\NotSupportedException;



use Yii;

/**
 * Signup form
 */
class SignupForm extends Model
{
    public $mobile;
    public $password;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['mobile', 'filter', 'filter' => 'trim'],
            ['mobile', 'required'],
            //['mobile', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
            ['mobile', 'string', 'min' => 11, 'max' => 11],
            ['password', 'required'],
            ['password', 'string', 'min' => 6],
        ];
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function signup()
    {
        if ($this->validate()) {
            $user = new User();
            $user->mobile = $this->mobile;
            $user->setPassword($this->password);
            $user->generateAuthKey();
            $user->setActiveAmount();
            $user->setUserHash();
            if ($user->save()) {
                return $user;
            }
        }

        return null;
    }

    public function validateBefore()
    {
        $this->active_amount = bcdiv(rand(501, 800), 1000, 3);
    }
}
